﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Usermanagment.VisualDisplay
{
	using Usermanagment.DBConnection;
	public partial class UserUpdate : Form
	{
		private int chips;
		private Boolean canc, canp;
		private Boolean cancFlag, canpFlag;
		private String uname;
		private DBConnect dbc;
		private DBDatafetch dft;
		private DBUpdate dbu;
		public UserUpdate()
		{
			InitializeComponent();
			this.MaximumSize = this.MinimumSize = this.Size;
			userName.Enabled = false;
			textBox1.Enabled = false;
		}

		public UserUpdate(DBConnect dbc)
			: this()
		{
			this.dbc = dbc;
			dft = new DBDatafetch(this.dbc);
			dbu = new DBUpdate(this.dbc);
			updateList();
		}

		private Boolean intToBool(int d)
		{
			return (d == 1) ? true : false;
		}

		private int boolToInt(Boolean d)
		{
			return (d) ? 1 : 0;
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			
		}

		private void updateList()
		{

			comboBox1.Items.Add("");
			DataTable d = dft.getAllUsers();
			foreach (DataRow row in d.Rows)
			{
				String s = String.Format("{0}", row.ItemArray[0]);
				comboBox1.Items.Add(s);
			}
		}


		private void btnUpdate_Click(object sender, EventArgs e)
		{
			int n = int.Parse(Chips.Text);
			if (n != chips)
				updateChips();
			if (cancFlag)
				updateCanc();
			if (canpFlag)
				updateCanp();
		}

		private void updateCanc()
		{
			canc = dbu.updateCancon(uname, boolToInt(canCon.Checked));
			if (canc)
				textBox1.Text = "CanConnect status updated for: " + uname;
			else
				textBox1.Text = "Cant update CanConnect status for: " + uname;
			wait();
		}

		private void updateCanp()
		{
			canp = dbu.updateCanplay(uname, boolToInt(canPly.Checked));
			if (canc)
				textBox1.Text = "CanPlay status updated for: " + uname;
			else
				textBox1.Text = "Cant update CanPlay status for: " + uname;
			wait();
		}
		private void updateChips()
		{
			canc = dbu.updateChips(uname, int.Parse(Chips.Text));
			if (canc)
				textBox1.Text = "Chips status updated for: " + uname;
			else
				textBox1.Text = "Cant update Chips status for: " + uname;
			wait();
		}

		private void wait()
		{
			for (int i = 0; i < 1000000; i++) ;
		}

		private void canCon_CheckedChanged(object sender, EventArgs e)
		{
			if (canCon.Checked != canc)
				cancFlag = true;
			else
				cancFlag = false;
		}

		private void canPly_CheckedChanged(object sender, EventArgs e)
		{
			if (canPly.Checked != canp)
				canpFlag = true;
			else
				canpFlag = false;

		}

		private void clearAll()
		{
			userName.Text = "";
			canPly.Checked = false;
			canCon.Checked = false;
			Chips.Text = "";
			comboBox1.SelectedIndex = 0;
			comboBox1.Items.Clear();
		}
		
		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			userName.Text = (uname = comboBox1.SelectedItem+ "");
			canCon.Checked = (canp = intToBool(dft.canPlay(uname)));
			canPly.Checked = (canc = intToBool(dft.canConnect(uname)));
			Chips.Text = (chips = dft.getChips(uname)) + "";

		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			dbu.deleteUser(uname);
			clearAll();
			updateList();
		}
	}
}
